//
//  FPRepaymentController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/2/8.
//

#import "FPRepaymentController.h"
#import "FPBorrowingAndRepaymentCell.h"
#import "FPLoanDetailController.h"
#import "FPBorrowingAndRepaymentModel.h"

@interface FPRepaymentController () <UITableViewDelegate , UITableViewDataSource , DZNEmptyDataSetSource , DZNEmptyDataSetDelegate>

@end

@implementation FPRepaymentController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)initUI {
    
//    [self setTitle:@"还款记录" style:(XYCustomNavigationStyleClear) hiddenBackBtn:NO];
    [self setTitle:@"申请记录" style:(XYCustomNavigationStyleClear) hiddenBackBtn:NO];
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.frame = CGRectMake(0.f, UIDevice.navigationFullHeight, SCREEN_WIDTH, SCREEN_HEIGHT - UIDevice.navigationFullHeight);
    
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.rowHeight = 82.f;
    [self.view addSubview:self.tableView];
    
    [self getData];
}

- (void)getData {
    
//    [MBManager showLoading];
    [FPBorrowingAndRepaymentModel getLoanListWithType:(LoanListTypeRepayment) success:^(NSInteger code, NSString * _Nonnull message, NSArray * _Nonnull list) {
        [MBManager hideLoading];
        
        [self.dataSource removeAllObjects];
        
        for (FPBorrowingAndRepaymentModel *model in list) {
            
            [self.dataSource addObject:model];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [self showError:error];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPBorrowingAndRepaymentCell *cell;
    
    if (cell == nil) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPBorrowingAndRepaymentCell.class) owner:nil options:nil].lastObject;
    }
    
    FPBorrowingAndRepaymentModel *model = self.dataSource[indexPath.row];
    
//    [cell setRepaymentListStatus:model.repaymentStatus.integerValue];
//    
//    NSString *time = [FPBorrowingAndRepaymentModel timeOfParam:model.createTime];
//    NSString *term = [FPBorrowingAndRepaymentModel termOfParam:model.lengthOfMaturity];
//    
//    cell.titleLab.text = [NSString stringWithFormat:@"借款金额：%@元",model.amount];
//    cell.subTitleLab.text = [NSString stringWithFormat:@"借款时间：%@ 丨 %@期",time , term];
    
    cell.titleLab.text = model.productName;
    cell.subTitleLab.text = model.createTime;
    cell.statusLab.text = model.status;
    cell.statusLab.textColor = [UIColor hexColor:@"#F12D2D"];
    
    cell.arrowImgView.hidden = YES;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    FPBorrowingAndRepaymentModel *model = self.dataSource[indexPath.row];
//    
//    FPLoanDetailController *vc = [[FPLoanDetailController alloc] init];
//    vc.loanID = model.ID;
//    [self.navigationController pushViewController:vc animated:YES];
}


- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    return IMG(@"bg_nodata");
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {

    return -100.f;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    
    NSString *text;
    
    text = @"暂无记录";
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [NSString stringToAttribute:attStr font:FONT(12.f) range:NSMakeRange(0, text.length)];
    [NSString stringToAttribute:attStr color:[UIColor hexColor:@"#6A6A6A"] range:NSMakeRange(0, text.length)];

    return attStr;
}

@end
